/**
  ******************************************************************************
  * @file    SPEED_DRIVER.c
  * @author  Feirui_Joe
  * @version V0.1
  * @date    07/26/2010
  * @brief   ACAD Handling
  ******************************************************************************
  * @copy
  *
  * <h2><center>&copy; COPYRIGHT 2010 Feirui</center></h2>
  */ 



/**
  * @brief  INCLUDE FILES.
  */
#ifndef	SPEED_DRIVER_GLOBALS
#define	SPEED_DRIVER_GLOBALS
#include <includes.h>




/************************************/
/*    Initial EngineSpeed Measure */
/************************************/

static void RCC_EngineSpeedCfg  (void);
static void GPIO_EngineSpeedCfg (void);
static void TIM_EngineSpeedCfg  (void);

/* Initial Freq Measure Config */
void EngineSpeedCfg_Init  (void)
{
  /* System Clocks Configuration */
  RCC_EngineSpeedCfg();

  /* Disable Timer Int   */
	TIM_EngineSpeedCtrALL(DISABLE);

  /* Configure the GPIO ports */
  GPIO_EngineSpeedCfg();
  
  /* Configure the Timer */
  TIM_EngineSpeedCfg();
  
  /* Enable Timer Int   */
  TIM_EngineSpeedCtrALL(ENABLE);
}



/**
  * @brief  Configures the different system clocks.
  * @param  None
  * @retval None
  */
static void RCC_EngineSpeedCfg(void)
{
  /* TIM clock enable */
  RCC_APB1PeriphClockCmd(ENGSPEED_TIMRCCPeriph, ENABLE);
  
  /* GPIOx and AFIO clock enable */
  RCC_APB2PeriphClockCmd(ENGSPEED_RCCGPIOPeriph | RCC_APB2Periph_AFIO, ENABLE);
}

/**
  * @brief  Configure the GPIOD Pins.
  * @param  None
  * @retval None
  */
static void GPIO_EngineSpeedCfg(void)
{
	GPIO_InitTypeDef GPIO_InitStructure;
	
  /* TIM Channel remapping pins */
  GPIO_PinRemapConfig(ENGSPEED_GPIO_PinRemap, ENABLE);
  
  /* Configuration Port for Freq Measure */
  GPIO_InitStructure.GPIO_Pin   = ENGSPEED_GPIOxBitx;
  GPIO_InitStructure.GPIO_Speed = ENGSPEED_GPIOSpeed;
  GPIO_InitStructure.GPIO_Mode  = ENGSPEED_GPIOModeI;
  GPIO_Init(ENGSPEED_GPIOxPort, &GPIO_InitStructure);
}


/**
  * @brief  Configure TIM.
  * @param  None
  * @retval None
  */
static void TIM_EngineSpeedCfg(void)
{
	TIM_OCInitTypeDef  TIM_OCInitStructure;
  TIM_ICInitTypeDef  TIM_ICInitStructure;
  TIM_TimeBaseInitTypeDef  TIM_TimeBaseStructure;
  
  /* TIM config */
  TIM_Cmd(ENGSPEED_TIM, DISABLE);
  
  /* Choose Internal Clock */
  TIM_InternalClockConfig(ENGSPEED_TIM);
  
  /* Time Base configuration */
  TIM_TimeBaseStructure.TIM_Prescaler         = ENGSPEED_TIMPrescaler        ;
  TIM_TimeBaseStructure.TIM_CounterMode       = ENGSPEED_TIMCounterMode      ;
  TIM_TimeBaseStructure.TIM_Period            = ENGSPEED_TIMPeriod           ;
  TIM_TimeBaseStructure.TIM_ClockDivision     = ENGSPEED_TIMClockDivision    ;
  TIM_TimeBaseStructure.TIM_RepetitionCounter = ENGSPEED_TIMRepetitionCounter;
  TIM_TimeBaseInit(ENGSPEED_TIM, &TIM_TimeBaseStructure);
  
  /* Input Capture config:Channel1 */
  TIM_ICInitStructure.TIM_Channel     = ENGSPEED_TIMChannel     ;
  TIM_ICInitStructure.TIM_ICPolarity  = ENGSPEED_TIM_ICPolarity ;
  TIM_ICInitStructure.TIM_ICSelection = ENGSPEED_TIM_ICSelection;
  TIM_ICInitStructure.TIM_ICPrescaler = ENGSPEED_TIM_ICPrescaler;
  TIM_ICInitStructure.TIM_ICFilter    = ENGSPEED_TIM_ICFilter   ;
  TIM_ICInit(ENGSPEED_TIMx, &TIM_ICInitStructure);
  
  /* Output Compare Active Mode configuration: Channel2 */
  TIM_OCInitStructure.TIM_OCMode       = ENGSPEED_TIMOCMode      ;
  TIM_OCInitStructure.TIM_OutputState  = ENGSPEED_TIMOutputState ;
  TIM_OCInitStructure.TIM_OutputNState = ENGSPEED_TIMOutputNState;
  TIM_OCInitStructure.TIM_Pulse        = ENGSPEED_TIMOCPulse     ;
  TIM_OCInitStructure.TIM_OCPolarity   = ENGSPEED_TIMOCPolarity  ;
  TIM_OCInitStructure.TIM_OCNPolarity  = ENGSPEED_TIMOCNPolarity ;
  TIM_OCInitStructure.TIM_OCIdleState  = ENGSPEED_TIMOCIdleState ;
  TIM_OCInitStructure.TIM_OCNIdleState = ENGSPEED_TIMOCNIdleState;
  TIM_OC2Init(ENGSPEED_TIM, &TIM_OCInitStructure);
  
  /* Enable reload CR1 after EVENT */
  TIM_OC2PreloadConfig(ENGSPEED_TIM, TIM_OCPreload_Disable);
  
  /* TIM enable/disable counter */
  TIM_Cmd(ENGSPEED_TIM, ENABLE);
}


/**
  * @brief  Control TIM.
  * @param  None
  * @retval None
  */
void TIM_EngineSpeedCtrALL(FunctionalState TIMCtrSta)
{
	NVIC_InitTypeDef NVIC_InitStructure;
  
  /* Disable the TIM Interrupt Request */
  TIM_ITConfig(ENGSPEED_TIM,ENGSPEED_ITx,DISABLE);
  
  /* Enable the TIMx CC global Interrupt */
  NVIC_InitStructure.NVIC_IRQChannel                   = ENGSPEED_IRQChannel;
  NVIC_InitStructure.NVIC_IRQChannelPreemptionPriority = ENGSPEED_IRQPrePrio;
  NVIC_InitStructure.NVIC_IRQChannelSubPriority        = ENGSPEED_IRQSubPrio;
  NVIC_InitStructure.NVIC_IRQChannelCmd                = TIMCtrSta;
  NVIC_Init(&NVIC_InitStructure);
  
  /* Initial TIM counter to 0 */
  TIM_SetCounter(ENGSPEED_TIM,0);
  
  /* TIM Interrupt config */
  /* Clear TIM Update Flag */
  TIM_ClearFlag(ENGSPEED_TIM,ENGSPEED_FLAGx);
  /* Clear TIM Update Pending Bit */
  TIM_ClearITPendingBit(ENGSPEED_TIM,ENGSPEED_ITx);
  /* Enable/disable the TIM Update Interrupt Request */
  TIM_ITConfig(ENGSPEED_TIM,ENGSPEED_ITx,TIMCtrSta);
}


















/*$PAGE*/#endif	/*	SPEED_DRIVER_GLOBALS	*/
